package com.uni.Result;

import lombok.Data;

@Data
public class Result<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;
    //構造私有化，不能讓外界進行new
    public Result(){}
    //封裝數據
    public static <T> Result<T> build(T body,ResultCodeEnum resultCodeEnum){
        Result<T> result = new Result<>();
        if(body != null){
            result.setData(body);
        }
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }


    //返回成功方法
    public static <T> Result<T> ok(){
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok(T data){
        return Result.build(data,ResultCodeEnum.SUCCESS);
    }

    //返回失敗的方法
    public static <T> Result<T> fail(){
        return Result.build(null,ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> fail(T data){
        return Result.build(data,ResultCodeEnum.FAIL);
    }
    public static <T> Result<T> fail(ResultCodeEnum resultCodeEnum){
        return Result.build(null,resultCodeEnum);
    }
    public static <T> Result<T> fail(Integer code,String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }

    //可以链式调用
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
    //使用的时候可以链式调用Result.fail().code().message()
    //也可以直接把枚举放到失败的里面，成功默认的是200
}
